
package com.nuvo.beans;

import com.nuvo.entities.Compra;
import com.nuvo.services.CompraService;
import java.io.Serializable;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import javax.faces.model.SelectItem;


@ManagedBean
@SessionScoped
public class CompraController implements Serializable{
    @EJB
    private CompraService compraService;
    
    
    private Compra selected;
    

    
    public CompraController() {
    }

    public Compra getSelected() {
        if(selected == null){
            selected = new Compra();
        }
        return selected;
    }
    
    public List <Compra> listado(){
        return compraService.findAll();
    }
    
    
    
    
    //----metodos del bean---//
    
    public String index(){
        return "/compra/index";
    }
   
    
    public String nuevo(){
        selected = new Compra();
    return "/compra/new";
    }
    
     public String agregar() {
        compraService.create(selected);
        return "/compra/index";
    } 
     
     public String guardar() {
        compraService.edit(selected);
        return "/compra/index";
    }
     
      public String editar(int codigo) {
        selected = compraService.find(codigo);
        return "/compra/edit";
    }
      
      public String eliminar(int codigo) {
        selected = compraService.find(codigo);
        compraService.remove(selected);
        return "/compra/index";
    } 
      
      
      // --------------------- Metodos de Ayuda para acceder al Bean por otras Clases ---------------------

    public SelectItem[] getItemsAvailableSelectOne() {
        return getSelectItems(compraService.findAll(), true);
    }

    // Genera una lista con los items seleccionados (uno o muchos segun selectOne). Para entidades relacionadas.
    public static SelectItem[] getSelectItems(List<?> entities, boolean selectOne) {

        int size = selectOne ? entities.size() + 1 : entities.size();
        SelectItem[] items = new SelectItem[size];
        int i = 0;
        if (selectOne) {
            items[0] = new SelectItem("", "seleccione uno");
            i++;
        }
        for (Object x : entities) {
            items[i++] = new SelectItem(x, x.toString());
        }
        return items;

    }
      
       // ------------------ Clase para conversiones se tiene que implementar todos los metodos ------------------
    @FacesConverter(forClass = Compra.class)
    public static class ArticuloControllerConverter implements Converter {

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        } 

        String getStringKey(java.lang.Integer value) {
            StringBuffer sb = new StringBuffer();
            sb.append(value);
            return sb.toString();
        } 

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) { return null; }
            CompraController controller = (CompraController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "compraController");
            return controller.compraService.find(getKey(value));
        } 

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) { return null; }
            if (object instanceof Compra) {
                Compra o = (Compra) object;
                return getStringKey(o.getId());
            }
            else { throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + CompraController.class.getName()); }
        } 

    
    }
      
      
      
}
